package us.icitap.cm.ui.policecase;

import java.util.List;
import java.util.Set;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListManadatoryValuesEnum;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.EventReport;
import us.icitap.cm.business.entities.PoliceCode;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.business.entities.UniqueNumber;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.listeners.ReportListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustTwinColSelect;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.OfficerField;
import us.icitap.cm.ui.shared.PoliceCodeField;
import us.icitap.cm.ui.shared.PoliceOperationField;
import us.icitap.cm.ui.shared.ReportLayout;
import us.icitap.cm.ui.shared.UniqueNumberField;
import us.icitap.cm.utils.HandleNotification;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.FormatingUtils;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;


public class EventReportDataLayout extends ReportLayout {
	
	private EventReport er;
	
	private CustComboBox erMethodComboBox = new CustComboBox(AppListTypeEnum.ReportingMethod.getCaption());
	private CustDateField fromDate = new CustDateField();
	private CustDateField toDate = new CustDateField("deri: ");

	private CheckBox domesticViolenceChekBox = new CheckBox("Dhune ne familje");
	private CheckBox remainAttemptCheckBox = new CheckBox("Mbetur ne tentative");
	private CustComboBox modusOperandusSelect = new CustComboBox(AppListTypeEnum.ModusOperandus.getCaption());
	private CustTwinColSelect toolUsedTwin = new CustTwinColSelect(AppListTypeEnum.ToolUsed.getCaption());
	private CustComboBox motivationSelect = new CustComboBox(AppListTypeEnum.Motivation.getCaption());
	private CustTwinColSelect consequencesTwin = new CustTwinColSelect(AppListTypeEnum.Consequence.getCaption());

	private UniqueNumberField uniqueNumberField = new UniqueNumberField();
	
	private CustComboBox investStartedMethodSelect = new CustComboBox(AppListTypeEnum.InvestigationStartedMethod.getCaption());
	private CustTextField investStartedDescTextField = new CustTextField(AppListTypeEnum.InvestigationStartedMethod.getCaption());
	
	private  CustComboBox investMethodSelect = new CustComboBox(AppListTypeEnum.InvestigationMethod.getCaption());
	private CustTextField investMethDescTextField = new CustTextField(AppListTypeEnum.InvestigationMethod.getCaption());
	
	private OfficerField officerField = new OfficerField("Raporti plotesohet nga");
	private PoliceCodeField policeCodeField; 
	private PoliceOperationField policeOperationField = new PoliceOperationField();
	
	private CustTextArea notesTextArea = new CustTextArea("Permbledhje e ngjarjes");
	
	private PlaceField placeField = new PlaceField();
	
	private final CustBeanFieldGroup<EventReport> binder = new CustBeanFieldGroup<EventReport>(EventReport.class);

	final CustomFormLayout cfl = new CustomFormLayout("650px", "Raport Ngjarje", "Reset", "Ruaj");
	
	private ReportListener listener;

	boolean editableVisible = false;
	boolean readOnlyVisible = false;
	
	final TabSheet tabSheet = new TabSheet();
	
	
	private void _init(){
		this.notesTextArea.setWidth("53em");
		this.notesTextArea.setRows(30);
		
		this.toolUsedTwin.setWidth("450px");
		this.consequencesTwin.setWidth("450px");
		
		fromDate.setResolution(Resolution.MINUTE);
		fromDate.setDateFormat(FormatingUtils.DATE_TIME_FORMAT);
		
		toDate.setResolution(Resolution.MINUTE);
		toDate.setDateFormat(FormatingUtils.DATE_TIME_FORMAT);
		
		this.investStartedMethodSelect.setWidth("450px");
		this.investStartedDescTextField.setWidth("450px");
		this.investMethodSelect.setWidth("450px");
		this.investMethDescTextField.setWidth("450px");
	}
	
	public EventReportDataLayout() {
		this.setMargin(true);	
		
		_init();
		
		
		Result<List<PoliceCode>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getRootPoliceCode();
		if(result.isSucceeded())
			policeCodeField = new PoliceCodeField(result.getObject());
		else
			policeCodeField = new PoliceCodeField();
		
		erMethodComboBox.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.ReportingMethod, false)));

		
		motivationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.Motivation, false)));

		
		modusOperandusSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.ModusOperandus, false)));

		
		toolUsedTwin.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.ToolUsed, false)));
		toolUsedTwin.setConverter(Set.class);
		
		consequencesTwin.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.Consequence, false)));
		consequencesTwin.setConverter(Set.class);
		
		investStartedMethodSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.InvestigationStartedMethod, false)));

		investMethodSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.InvestigationMethod, false)));
		
		FormLayout fl = new FormLayout();
		fl.addComponent(erMethodComboBox);
		fl.addComponent(uniqueNumberField);
		uniqueNumberField.setImmediate(true);
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.setCaption("Intervali kohor: ");
		hl.addComponent(fromDate);
		hl.setComponentAlignment(fromDate, Alignment.BOTTOM_LEFT);
		FormLayout toDateFl = new FormLayout();
		toDateFl.setMargin(false);
		toDateFl.addComponent(toDate);
		hl.addComponent(toDateFl);
		
		fl.addComponent(hl);
		
		fl.addComponent(this.policeCodeField);
		fl.addComponent(this.domesticViolenceChekBox);
		fl.addComponent(this.remainAttemptCheckBox);
		fl.addComponent(this.officerField);
		
		tabSheet.addTab(fl, "Ngjarja");
		
		fl = new FormLayout();
		
		fl.addComponent(this.motivationSelect);
		fl.addComponent(this.modusOperandusSelect);
		fl.addComponent(this.toolUsedTwin);
		fl.addComponent(this.consequencesTwin);
		fl.addComponent(this.investStartedMethodSelect);
		fl.addComponent(this.investStartedDescTextField);
		fl.addComponent(this.investMethodSelect);
		fl.addComponent(this.investMethDescTextField);
		fl.addComponent(this.policeOperationField);
		
	
		tabSheet.addTab(fl, "Info shtese");
		tabSheet.addTab(placeField, "Adresa");
		
		VerticalLayout vl = new VerticalLayout();
		vl.addComponent(notesTextArea);
		
		
		tabSheet.addTab(vl, "Permbledhje");
		
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		
		

		
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
	
					binder.commit();

					if(er.getReport().getPoliceOperation() != null)
						er.getReport().getPoliceOperation().setReport(er.getReport());
					
					if(policeCodeField.getValue() == null || policeCodeField.getValue().isEmpty()){
						HandleNotification.showWarningOnScreen("Duhet te zgjidhni te pakten nje element nga kodi policor");
						return;
					}
					else{
						er.getReport().setPrimaryPoliceCode(er.getReport().getPoliceCodeList().get(0));
					}
						
					if(EventReportDataLayout.this.listener != null)
						EventReportDataLayout.this.listener.storeInMemory(er.getReport());
					
					cfl.setRightButtonOkIcon();
				} catch (CommitException e) {

					e.printStackTrace();
				}
				
			}
		});
		this.erMethodComboBox.setImmediate(true);
		this.erMethodComboBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() == null || event.getProperty().getValue() == null) return;
				System.out.println(event.getProperty().getValue());
				Result<AppList> result = CaseManagementUI.appStatelessBean.getPersistenceBean().findAppListByTag(AppListManadatoryValuesEnum.ReportingMethod_KALLEZIM.getCaption());
				
				if(result.isSucceeded()){
					if(event.getProperty().getValue().equals(result.getObject())){
						UniqueNumber un = new UniqueNumber();
						un.setOrgStructure(er.getReport().getOrgStructure());
						un.setRequestingOfficer(er.getReport().getReportOfficer());
						un.setUnGenerated(false);
						un.setUniqueNumber("Numri unik do te gjenerohet pas ruajtjes");
						uniqueNumberField.setValue(un);
					}
					else uniqueNumberField.setValue(er.getUniqueNumber());
					
						
				}
				else uniqueNumberField.setValue(er.getUniqueNumber());
				
				
					
				
			}
		});
		
		
		binder.bind(uniqueNumberField, "uniqueNumber");
		binder.bind(erMethodComboBox, "reportingMethod");
		binder.bind(fromDate, "eventTimeFrom");
		binder.bind(toDate, "eventTimeTo");
		binder.bind(domesticViolenceChekBox, "domesticViolence");
		binder.bind(remainAttemptCheckBox, "remainedAttempt");
		binder.bind(motivationSelect, "motivation");
		binder.bind(modusOperandusSelect, "modusOperandus");
		binder.bind(toolUsedTwin, "toolUsedList");
		binder.bind(consequencesTwin, "consequenceList");
		binder.bind(investStartedMethodSelect, "report.investStartedMethod");
		binder.bind(investStartedDescTextField, "report.investStartedDescription");
		binder.bind(investMethodSelect, "report.investigationMethod");
		binder.bind(investMethDescTextField, "report.investMethodDescription");
		binder.bind(officerField, "report.reportOfficer");
		binder.bind(policeOperationField, "report.policeOperation");
		binder.bind(policeCodeField, "report.policeCodeList");
		binder.bind(notesTextArea, "report.notes");
		binder.bind(placeField, "place");
		
		
	}

	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = new VerticalLayout(); //.showER(er);
		vl.setCaption("Raport Ngjarje");
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	
	
	public void setEventReport(EventReport er){
		this.er = er;
	}

	@Override
	public void addReport(Report report) {
		er = new EventReport();
		AppEntity appEntity = new AppEntity(report.getRecordUser(), EntityTypeEnum.PLACE);
		er.setPlace(appEntity.getPlace());
		er.setReport(report);
		report.setEventReport(er);
		binder.setItemDataSource(er);
		officerField.setEnabled(false);

		this.showEditableForm();
		
	}



	@Override
	public void addReportListener(ReportListener listener) {
		this.listener = listener;
		
	}



	@Override
	public void loadReport(Report report, boolean editable) {
		this.er = report.getEventReport();
		binder.setItemDataSource(er);
		
		if(editable) 
			this.showEditableForm();
		
		else
			this.showReadOnlyForm();
	}
	

}
